﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WestJourney.Properties;
using System.Threading;
using WestJourney.Misc;

namespace WestJourney
{
    public partial class MainForm : Form
    {
        private Dictionary<string, bool> userList = new Dictionary<string, bool>();
        private int currentUserIndex = 0;
        private List<string> userNameList
        {
            get
            {
                List<string> list = new List<string>();
                foreach (KeyValuePair<string, bool> kvp in userList)
                {
                    list.Add(kvp.Key);
                }
                return list;
            }
        }

        BackgroundWorker bgWorker = new BackgroundWorker();

        public MainForm()
        {
            //CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            this.LoadInformation();
        }

        private void LoadInformation()
        {
            this.LoadUsers();
            this.LoadBackGroundWorker();
        }

        private void LoadBackGroundWorker()
        {
            bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
            bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted);
        }

        private void MoveStep(int stepCount)
        {
            string userName = userNameList[currentUserIndex];
            string controlName = "user" + userName;
            Control[] controls = flowLayoutPanel.Controls.Find(controlName, false);
            if (controls != null)
            {
                for (int i = 0; i < controls.Length; i++)
                {
                    UserDisplay userDisplay = controls[i] as UserDisplay;
                    if (userDisplay != null)
                    {
                        userDisplay.MoveStep(stepCount);
                    }
                }
            }
        }

        void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            int stepCount = 1;

            for (int i = 0; i < 10; i++)
            {
                stepCount = GetRandomStep();
                Image stepImage = GetStepImage(stepCount);
                this.pictureBox.Image = stepImage;
            }

            e.Result = stepCount;
        }

        void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            int stepCount = Convert.ToInt32(e.Result);
            this.labelInformation.Text = string.Format("{0}向前走{1}步", userNameList[currentUserIndex].ToString(), stepCount);

            MoveStep(stepCount);
            currentUserIndex++;
            if (currentUserIndex >= userNameList.Count)
            {
                currentUserIndex = 0;
            }
            this.labelNext.Text = string.Format("接下来，该{0}投掷了", userNameList[currentUserIndex]);
            if (userList[userNameList[currentUserIndex]] == true)
            {
                this.Play();
            }
        }

        private void buttonPlay_Click(object sender, EventArgs e)
        {
            this.Play();
        }

        private void Play()
        {
            bgWorker.RunWorkerAsync();
        }

        private void LoadUsers()
        {
            string users = Settings.Default.Users;
            bool userComputer = Settings.Default.UseComputer;
            string[] userArray = users.Split(new char[] { ',', '，', ';', '；' }, StringSplitOptions.RemoveEmptyEntries);
            if (userArray == null || userArray.Length == 0)
            {
                userArray = new string[1];
                userArray[0] = "默认用户";
            }

            if (userArray.Length == 1)
            {
                userComputer = true;
            }

            foreach (string userName in userArray)
            {
                UserDisplay user = new UserDisplay();
                user.Name = "user" + userName;
                user.UserName = userName;
                user.MoveFinished += new EventHandler<Misc.UserFinishedEventArgs>(user_MoveFinished);

                userList.Add(userName, false);
                this.flowLayoutPanel.Controls.Add(user);
            }

            if (userComputer == true)
            {
                UserDisplay user = new UserDisplay();
                user.Name = "user" + "佛主";
                user.UserName = "佛主";
                user.MoveFinished += new EventHandler<Misc.UserFinishedEventArgs>(user_MoveFinished);

                userList.Add("佛主", true);
                this.flowLayoutPanel.Controls.Add(user);
            }
        }

        void user_MoveFinished(object sender, Misc.UserFinishedEventArgs e)
        {
            this.labelInformation.Text= string.Format("本轮{0}获得了胜利",e.UserName);
            this.buttonPlay.Enabled = false;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.DisplayInformation();
        }

        private void DisplayInformation()
        {
            this.labelNext.Text = string.Format("接下来，该{0}投掷了", userNameList[currentUserIndex]);
        }

        private int GetRandomStep()
        {
            Thread.Sleep(200);
            Random r = new Random();
            return r.Next(1, 6);
        }

        private Image GetStepImage(int stepNumber)
        {
            switch (stepNumber)
            {
                case 1:
                    return Resources.shai_1;
                case 2:
                    return Resources.shai_2;
                case 3:
                    return Resources.shai_3;
                case 4:
                    return Resources.shai_4;
                case 5:
                    return Resources.shai_5;
                case 6:
                default:
                    return Resources.shai_6;
            }
        }
    }
}
